package com.che.libcommon.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.DrawableRes;
import android.support.annotation.RawRes;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import com.che.common.libcommon.R;
import java.util.List;

/**
 * Created by zl on 2017/12/1.
 */

public class BottomNavLayout extends LinearLayoutCompat implements OnClickListener{

  public BottomNavLayout(Context context) {
    this(context,null);
  }

  public BottomNavLayout(Context context, AttributeSet attrs) {
    this(context, attrs,0);
  }

  public BottomNavLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    setOrientation(HORIZONTAL);

  }


  private OnTabSelectedListener mOnTabSelectedListener;
  private ItemViewProvider itemViewProvider;

  public void setOnTabSelectedListener(
      OnTabSelectedListener onTabSelectedListener) {
    this.mOnTabSelectedListener = onTabSelectedListener;
  }


  public void setItems(List<BottomItem> items,ItemViewProvider itemViewProvider){
    setItems(items, itemViewProvider,0);
  }

  public void updateItem(int index,@StringRes int textRes){
    BottomItem item = getItem(index);
    if (item != null && item.titleRes != textRes){
      item.titleRes = textRes;
      View childAt = getChildAt(index);
      if (childAt != null){
        itemViewProvider.getView(this,childAt,index,item);
      }
    }
  }

  public BottomItem getItem(int index){
    View childAt = getChildAt(index);
    if (childAt != null){
      Object tag = childAt.getTag(R.id.item_data);

      if (tag instanceof BottomItem){
        return ((BottomItem) tag);
      }
    }
    return null;
  }

  public void setItems(List<BottomItem> items,ItemViewProvider itemViewProvider,int defSelection){
    removeAllViewsInLayout();
    this.itemViewProvider = itemViewProvider;
    int index = 0;
    for (BottomItem item : items) {
      LayoutParams lp = new LayoutParams(0, LayoutParams.MATCH_PARENT, 1);

      View itemLayout = itemViewProvider.getView(this,null,index,item);
      addViewInLayout(itemLayout,-1,  lp);
      itemLayout.setOnClickListener(this);
      itemLayout.setTag(R.id.item_data,item);
      itemLayout.setTag(R.id.item_position,index);
      if(index == defSelection){
        itemLayout.setSelected(true);
        selectedPosition = defSelection;
      }else {
        itemLayout.setSelected(false);
      }
      index++;
    }
    requestLayout();
  }

  public int getSelectedPosition() {
    return selectedPosition;
  }

//  private View createItemLayout(BottomItem item,int position){
//    View inflate = inflate(getContext(), R.layout.item_bottom_nav, null);
//    inflate.setClickable(true);
//    inflate.setOnClickListener(this);
//    inflate.setTag(R.id.item_data,item);
//    inflate.setTag(R.id.item_position,position);
//
//    ImageView imageView = inflate.findViewById(R.id.img_icon);
//    TextView textView = inflate.findViewById(R.id.tv_title);
//    imageView.setImageResource(item.drawableRes);
//    textView.setText(item.titleRes);
//
//    return inflate;
//  }


  private int selectedPosition = -1;

  @Override
  public void onClick(View v) {
    Object tag = v.getTag(R.id.item_position);
    if(tag instanceof Integer){
      int pos = ((Integer) tag);
      setSelectedPosition(pos);
      BottomItem item = (BottomItem) v.getTag(R.id.item_data);
      if(mOnTabSelectedListener != null && item != null){
        mOnTabSelectedListener.onSelected(v,item);
      }
    }
  }

  public void setSelectedPosition(int pos){
    setSelectedPosition(pos,false);
  }

  public void setSelectedPosition(int pos,boolean callCallback){
    if(pos != selectedPosition){
      //
      setStatus(selectedPosition,false);
      setStatus(pos,true);

      selectedPosition = pos;

      if (callCallback){
        BottomItem item = getItem(pos);
        if(mOnTabSelectedListener != null && item != null){
          mOnTabSelectedListener.onSelected(getChildAt(pos),item);
        }
      }

    }
  }

  private void setStatus(int position,boolean selected){
    getChildAt(position).setSelected(selected);
  }



  public interface OnTabSelectedListener{
    void onSelected(View view,BottomItem item);
  }


  public interface ItemViewProvider{
    View getView(ViewGroup parent,View convertView,int position,BottomItem item);
  }

  public static class BottomItem {

    public
    @StringRes
    int titleRes = 0;
    public
    @DrawableRes
    int drawableRes = 0;

    @RawRes
    public int animRes = 0;

    public String tag;


    public BottomItem(int titleRes, int drawableRes,int animRes, String tag) {
      this.titleRes = titleRes;
      this.drawableRes = drawableRes;
      this.animRes = animRes;
      this.tag = tag;
    }

  }
}
